/*
科技树
*/
window._MXLU = {

    _MAINLABEL:null,

    _SELECTEDCHAPTER:null,

    desc:[
            "全局修为说明:",
            "1.每解锁一个关卡都可以为其解锁一个对应的修为",
            "2.关卡会掉落对应绑定的修为",
            "3.升级修为可以提高玩家的修为上限",
            "3.升级修为会增加部分商店的商品",
            "4.升级需要消耗木头,所需木头数等于等级,木头可以在地图->列表->成就中领取"
        ],

    _lvTotalWood:{
        '1':0,
        '2':2,
        '3':5,
        '4':9,
        '5':14,
        '6':20,
        '7':27,
        '8':35,
        '9':44,
        '10':54,
    },

    _lvWood:[
        0,
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8,
        9,
        10
    ],

    showDesc:function(node){
        node.getChildByName('xiangqing')
            .getChildByName('cengjiu')
            .getChildByName('message')
            .getComponent(cc.RichText).string = _MXLU.desc.join('\n');
    },

    showUi:function(parentNode){

        var uiObj = _GRM.getNodeObj('globalxw');
        var mainLabel = uiObj.node;
        _MXLU._MAINLABEL = mainLabel;

        if(uiObj.isnew){
            _MXLU.initButton(mainLabel);
            _MXLU.showDesc(mainLabel);
        }

        parentNode.addChild(mainLabel);

        _MXLU.refresh();

        return mainLabel;

    },

    initButton:function(node){
        node.getChildByName('mask').on(cc.Node.EventType.TOUCH_START, function(){
            _GRM.putNode(this);
        }, node);

        var labels = node.getChildByName('cityMap')
                               .getChildByName('scrollview')
                               .getChildByName('view')
                               .getChildByName('content')
                               .getChildren();
        for(var i=0;i<labels.length;i++){
            var labelBtn = labels[i].getChildByName('button');
            labelBtn.on(cc.Node.EventType.TOUCH_START, function(){
                console.log('解锁',this.chapterName);
                _MXLU.showXwSelect(this.chapterName);
            }, labels[i]);

            var labelBtncj = labels[i].getChildByName('buttoncj');
            labelBtncj.on(cc.Node.EventType.TOUCH_START, function(){
                console.log('升级',this.chapterName);
                _MXLU.levelUpXw(this.chapterName);
            }, labels[i]);
        }

        var xwSelectNodes = node.getChildByName('selectLabel')
                               .getChildByName('xwSelect')
                               .getChildren();
        for(var i=0;i<xwSelectNodes.length;i++){
            var xwSelectNode = xwSelectNodes[i];
            xwSelectNode.on(cc.Node.EventType.TOUCH_START, function(){
                console.log('修为选择',this.xw,_MXLU._SELECTEDCHAPTER);
                _MXLU.signXw(_MXLU._SELECTEDCHAPTER,this.xw);
                _MXLU.refresh();
            }, xwSelectNode);
        }

        var xwMask = node.getChildByName('selectLabel')
                                .getChildByName('mask');
        xwMask.on(cc.Node.EventType.TOUCH_START, function(){
            this.active = false;
        }, node.getChildByName('selectLabel'));

        var btnMaplist = node.getChildByName('cityMap').getChildByName('btnToBm');
        btnMaplist.on(cc.Node.EventType.TOUCH_START, function(){
            this.getChildByName('xiangqing').active = true;
        }, node);

        var btnToWood = node.getChildByName('cityMap').getChildByName('btnToWood');
        btnToWood.on(cc.Node.EventType.TOUCH_START, function(){
            this.getChildByName('wood').active = true;
        }, node);

        var btnToReset = node.getChildByName('cityMap').getChildByName('btnToReset');
        btnToReset.on(cc.Node.EventType.TOUCH_START, function(){
            //_MXLU.resetAllXw();
            _CHOICEMANAGER.showChoice(_MAP.node,'resetXw',null);
        }, node);

        node.getChildByName('xiangqing').getChildByName('mask')
        .on(cc.Node.EventType.TOUCH_START, function(){
            this.getChildByName('xiangqing').active = false;;
        }, node);

        node.getChildByName('wood').getChildByName('mask')
        .on(cc.Node.EventType.TOUCH_START, function(){
            this.getChildByName('wood').active = false;;
        }, node);
    },

    showXwSelect:function(chapterName){
        var xwlv = _MXLU.getAllXw();
        _MXLU._SELECTEDCHAPTER = chapterName;
        _MXLU._MAINLABEL.getChildByName('selectLabel').active = true;

        var xwSelectNodes = _MXLU._MAINLABEL.getChildByName('selectLabel')
                               .getChildByName('xwSelect')
                               .getChildren();
        for(var i=0;i<xwSelectNodes.length;i++){
            var xwSelectNode = xwSelectNodes[i];
            xwSelectNode.active = false;
        }

        var nodeIndex = 0;
        for(var key in _XW){

            var tempNode = xwSelectNodes[nodeIndex];

            if(tempNode){
                if(xwlv[key]){
                    tempNode.active = false;
                }else{
                    tempNode.active = true;
                }
                tempNode.xw = key;
                tempNode.getComponent(cc.Sprite).spriteFrame = _GRM.getEquipFrame(_XW[key].img);
                tempNode.getChildByName('name').getComponent(cc.Label).string = _XW[key].label;
            }
            nodeIndex++;
        }
        

    },

    refresh:function(){

        _MXLU._MAINLABEL.getChildByName('selectLabel').active = false;

        var labels = _MXLU._MAINLABEL.getChildByName('cityMap')
                    .getChildByName('scrollview')
                    .getChildByName('view')
                    .getChildByName('content')
                    .getChildren();

        for(var i=0;i<labels.length;i++){
            labels[i].active = false;
        }

        var labelindex = 0;

        for(var key in _CMAP){

            labels[labelindex].active = true;
            var chapterInfo = null;
            var chapterXwInfo = _MXLU.getXwByChapter(key);
            var chapterXw = chapterXwInfo?chapterXwInfo.xw:null;
            var xw = chapterXw?_XW[chapterXw].label:'无';


            if(chapterInfo){
                labels[labelindex].getChildByName('nmb').getComponent(cc.Label).string = labelindex+"."+key;
                labels[labelindex].getChildByName('cityname').getComponent(cc.Label).string = xw;
                labels[labelindex].getChildByName('bossname').getComponent(cc.Label).string = chapterXwInfo.lv;

                if(chapterXw==null){
                    labels[labelindex].getChildByName('button').active = true;
                    labels[labelindex].getChildByName('buttoncj').active = false;
                }else{
                    labels[labelindex].getChildByName('button').active = false;
                    if(chapterXwInfo.lv<10){
                        labels[labelindex].getChildByName('buttoncj').active = true;
                    }else{
                        labels[labelindex].getChildByName('buttoncj').active = false;
                    }
                }
                
            }else{

                labels[labelindex].getChildByName('nmb').getComponent(cc.Label).string = labelindex+".???";
                labels[labelindex].getChildByName('cityname').getComponent(cc.Label).string = "???";
                labels[labelindex].getChildByName('bossname').getComponent(cc.Label).string = 0;
                labels[labelindex].getChildByName('button').active = false;
                labels[labelindex].getChildByName('buttoncj').active = false;
                //labels[labelindex].getChildByName('buttonxq').active = false;
            }

            labels[labelindex].chapterName = key;

            labelindex++;
        }

    },

    getXWLV:function(){

        if(window.gameData){
            if(!window.gameData.xwlv){
                window.gameData.xwlv=[];
                for(var key in window._CMAP){
                    window.gameData.xwlv.push({chapter:key,lv:0,xw:null});
                }
            }
        }

        return window.gameData.xwlv;
    },

    //获取科技树
    getAllXw:function(){

        var xwlv = _MXLU.getXWLV();
        var xwObj = {};
        for(var i=0;i<xwlv.length;i++){
            xwObj[xwlv[i].xw]=xwlv[i].lv*100;
        }
        return xwObj;
    },

    //根据关卡获得修为掉落
    getXwByChapter:function(chapter,xw){

        var xwlv = _MXLU.getXWLV();
        for(var i=0;i<xwlv.length;i++){
            if(xwlv[i].chapter==chapter){
                return xwlv[i];
            }
        }

    },

    //解锁修为
    signXw:function(chapter,xw){

        _TTCH.doCompleteTask('getXw',chapter,true);

        var xwlv = _MXLU.getXWLV();
        for(var i=0;i<xwlv.length;i++){
            if(xwlv[i].chapter==chapter){
                xwlv[i].xw = xw;
                xwlv[i].lv = 1;
                return;
            }
        }

    },

    //修为升级
    levelUpXw:function(chapter){

        var xwlv = _MXLU.getXWLV();
        for(var i=0;i<xwlv.length;i++){
            if(xwlv[i].chapter==chapter){
                var next = xwlv[i].lv+1;
                var wood = _MXLU._lvWood[next];
                if(wood){
                    if(_GAMEDATAHELPER.oprWood(-wood)){
                        xwlv[i].lv++;
                        _MXLU.refresh();
                    }else{
                        _TIPS.showMessage('升级需要木头:'+wood+',数量不足无法升级(木头可以在地图->列表->成就中领取)');
                    }
                }else{
                    _TIPS.showMessage('已满级');
                }
                return;
            }
        }

    },

    //重置修为 {chapter:key,lv:0,xw:null}
    resetAllXw:function(){

        var xwlv = _MXLU.getXWLV();

        var totalwood = 0;

        for(var i=0;i<xwlv.length;i++){

            var tempObj = xwlv[i];
            if(tempObj.xw&&tempObj.lv>=1){
                totalwood+=_MXLU._lvTotalWood[tempObj.lv];
            }

            tempObj.xw=null;
            tempObj.lv=0;
        }

        //清理主力
        for(var i=0;i<window.gameData.playerTeam.length;i++){

            var tempplayer = window.gameData.playerTeam[i];

            if(tempplayer.equip&&tempplayer.equip.length>0){
                for(var j=0;j<tempplayer.equip.length;j++){
                    _GAMEDATAHELPER.pushItem(tempplayer.equip[j]);
                }
            }

            if(_NPC[tempplayer.name]){
                var role = _CROF.createNpcRole(tempplayer.name,{dzlevel:0,lv:1,chapterLv:0,isHecheng:1})
                role.isboss = false;
                role.exp = tempplayer.exp;
                role.equip = [];
                window.gameData.playerTeam[i]=role;
            }else{
                tempplayer.face = "无业";
                tempplayer.pf = "无业";
                tempplayer.job =  "无业";
                if(!tempplayer.isMainRole){
                    tempplayer.skill = ['无业游民'];
                }else{
                    tempplayer.skill = ['主角光环','无业游民'];
                }
                tempplayer.equip = [];
            }
            tempplayer.xw = {};
        }

        //清理替补
        for(var i=0;i<window.gameData.playerTeamBack.length;i++){

            var tempplayer = window.gameData.playerTeamBack[i];

            if(tempplayer.equip&&tempplayer.equip.length>0){
                for(var j=0;j<tempplayer.equip.length;j++){
                    _GAMEDATAHELPER.pushItem(tempplayer.equip[j]);
                }
            }

            if(_NPC[tempplayer.name]){
                var role = _CROF.createNpcRole(tempplayer.name,{dzlevel:0,lv:1,chapterLv:0,isHecheng:1})
                role.isboss = false;
                role.exp = tempplayer.exp;
                role.equip = [];
                window.gameData.playerTeamBack[i]=role;
            }else{
                tempplayer.face = "无业";
                tempplayer.pf = "无业";
                tempplayer.job =  "无业";
                if(!tempplayer.isMainRole){
                    tempplayer.skill = ['无业游民'];
                }else{
                    tempplayer.skill = ['主角光环','无业游民'];
                }
                tempplayer.equip = [];
            }
            tempplayer.xw = {};
        }

        //重置酒馆
        window.gameData.jgArr=[];

        _GAMEDATAHELPER.oprWood(totalwood);
        _TIPS.showMessage("重置全局修为,获得木头:"+totalwood);
        _MXLU.refresh();

    },

    //根据修为获取各个商人商品
    getObjArrOfXw:function(shop){

        var objarr = [];
        var xwlv = _MXLU.getAllXw();

        if(shop=="酒馆商"){

            for(var key in _JOB){
                
                if(
                    XWH.isXwEnough2(_JOB[key].xw,xwlv)
                ){
                    objarr.push(key);
                }

            }

        }else if(shop=="装备商"){

            for(var key in _EQUIP){
                
                if(
                    XWH.isXwEnough2(_EQUIP[key].xw,xwlv)
                ){
                    objarr.push(key);
                }

            }

        }else if(shop=="技能商"){

            for(var key in _SKILL){
                
                if(
                    XWH.isXwEnough2(_SKILL[key].xw,xwlv)
                    &&!_SKILL[key].exc
                    &&!_SKILL[key].noshop
                    &&!_SKILL[key].bossflag
                ){
                    objarr.push(key);
                }

            }

        }else if(shop=="被动技能商"){

            for(var key in _SKILLBD){
                
                if(
                    XWH.isXwEnough2(_SKILLBD[key].xw,xwlv)
                    &&!_SKILLBD[key].noshop
                    &&!_SKILLBD[key].exc
                    &&!_SKILLBD[key].bossflag
                ){
                    objarr.push(key);
                }

            }

        }else if(shop=="修为商"){

            for(var key in _FIGHTITEM){
                if(_FIGHTITEM[key].skilltype=="xwbook"&&xwlv[_FIGHTITEM[key].xw]){
                    objarr.push(key);
                }
            }

        }

        return objarr;
    }

}